<template>
  <el-dialog
    title="工作日设置"
    :visible.sync="dialogVisible"
    :modal="true"
    :appendToBody="true"
    width="40%"
    v-dialog-drag
    :close-on-click-modal="false"
    top="5vh"
    class="report-text-form"
    @close="handleClose()"
    @open="handleOpen()"
  >
    <div class="dia-sub">
      <div>
        <span>{{weekData.year}}年</span>
        <span>{{weekData.month}}月</span>
        <span>{{weekData.day}}日</span>
      </div>
      <span class="week">{{weekData.week}}</span>
      <span>{{baseData.factoryName}}</span>
      <span v-if='baseData.workshopName'>-{{baseData.workshopName}}</span>
      <span v-if='baseData.lineName'>-{{baseData.lineName}} </span>
    </div>
    <el-form label-position="right" ref="ruleForm" label-width="200px" v-loading="saveloading">
      <template v-if='teamList && teamList.length'>
        <el-form-item
          v-for="item in teamList"
          :key="item.id"
          :label="item.workTime">
            <el-radio-group v-model="item.produceStatus">
              <el-radio label="ON">工作</el-radio>
              <el-radio label="OFF">休息</el-radio>
            </el-radio-group>
        </el-form-item>
      </template>
    </el-form>

    <div class="tips" v-if='!baseData.lineName'>设置成功后，将生效于其下所有
      <span v-if='baseData.workshopName'>产线</span>
      <span v-else>车间、产线</span>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" :loading="saveloading" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import {listClasses} from "@/api/team/classes";
  import { changeLineProduceStatus } from "@/api/shifts/userChange";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    weekData:{
      type: Object,
      default: ()=>{}
    },
    baseData:{
      type: Object,
      default: ()=>{}
    },
    weekdayItem:{
      type: Object,
      default: ()=>{}
    }
  },
  data() {
    return {
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 100,
      },
      teamList:[],
      saveloading: false
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val)
      },
      get() {
        return this.visible
      }
    }
  },
  methods: {
    handleClose() {
      this.saveloading = false
      this.resetFormData()
    },

    handleOpen() {
      this.resetFormData();
      this.teamList = structuredClone(this.weekdayItem?.classesDutyList);
      if(!this.teamList?.length){//没有配置工作日
        this.getList()
      }
      else{
        this.teamList.forEach(item => {//获取班次
          if(item?.workshopDutyInfoList?.length){
            item.workshopDutyInfoList.forEach(ele => {//获取车间安排
              if(ele?.lineDutyInfoList?.length){//获取车间下面产线安排
                if(!ele.lineDutyInfoList.some(this.isExistRest)){//所有产线都是休息日
                  this.$set(ele,"produceStatus","OFF");
                }
                else{
                  this.$set(ele,"produceStatus","ON");
                }
              }
              else{
                this.$set(ele,"produceStatus","ON");
              }
            })

            if(item.workshopDutyInfoList.every(this.isExisStatus) && !item.workshopDutyInfoList.some(this.isExistRest)){//所有s车间存在produceStatus，并且所有车间都是休息日
              this.$set(item,"produceStatus","OFF");
            }
            else{
              this.$set(item,"produceStatus","ON");
            }
          }
          else{
            this.$set(item,"produceStatus","ON");
          }

          this.$nextTick(()=>{
            this.$set(item,"workTime",`${item.classesName}(${item.startTime}-${item.endTime})`);
            this.$set(item,"id",item.classesId);
          })
        })
      }
    },

    isExistRest(value, index, ar){
      if(value?.produceStatus == "ON") {
        return true;
      }
      else {
        return false;
      }
    },

    isExisStatus(value, index, ar){
      if(value?.produceStatus){
        return true;
      }
      else{
        return false;
      }
    },

    getList() {
      this.saveloading = true;
      this.single = null;
      const { pageNum, pageSize } = this.pager
      const params = {
        query:{
          searchValue: ""
        },
        pageNum,
        pageSize
      }
      listClasses(params)
      .then(response => {
        if (response.code == 200 && response?.rows?.length) {
          this.teamList = response.rows || [];
          this.teamList.forEach(ele => {
            this.$nextTick(()=>{
              this.$set(ele,"workTime",`${ele.classesName}(${ele.startTime}-${ele.endTime})`);
              this.$set(ele,"produceStatus","ON");
            })
          })
        } else {
          this.teamList = []
        }
        this.saveloading = false;
      }).catch(() => {
        this.teamList = []
        this.saveloading = false;
      });
    },

    handleSubmit() {
      this.saveloading = true
      const{factoryDeptId,workshopId,lineId} = this.baseData;
      const {year,month,day} = this.weekData;
      const parmas = {
        factoryDeptId,
        workshopId,
        lineId,
        yearStr:year,
        monthStr:month,
        dayNo:day,
        classesProduceStatusList:this.teamList?.length ? this.teamList.map(ele => {
          return {
            classesId:ele?.id,
            classesName:ele?.classesName,
            startTime:ele?.startTime,
            endTime:ele?.endTime,
            produceStatus:ele?.produceStatus
          }
        }):[]
      }
      changeLineProduceStatus(parmas)
      .then((res) => {
        if(res.code == 200){
          this.$emit("submit-success")
          this.msgSuccess("操作成功");
          this.saveloading = false;
          this.dialogVisible = false;
        }
        else{
          this.msgError("操作失败")
        }
      })
      .catch(() => {
        this.msgError("操作失败")
        this.saveloading = false;
      });
    },

    resetFormData() {
      this.$refs.ruleForm?.resetFields();
      this.teamList = [];
    },
  }
};
</script>
<style lang="scss" scoped>
.report-text-form{
  .el-form{
    margin-top: 20px;
  }
  .dia-sub{
    display: flex;
    align-items: center;
    margin-left: 26px;
    font-size: 15px;
    font-weight: 600;
    .week{
      margin:0 8px
    }
  }
  .tips{
    color:#f56c6c;
    margin-left: 26px;
  }
}

</style>
